<template>
	<view class="mine-distribution-index">
		
		<view mode="aspectFill" lazy-load class="mine-index-bg abs"></view>	
		<view class="space-user-info rel"></view>
		<view class="apply-info ml-lg mr-lg fill-base f-title radius-24" style="z-index:999 !important;position: absolute;">
			<view class="flex-center f-md-title text-bold c-black">登录</view>
			<view class="space-lg"></view>
			<view class="pt-lg pb-md b-1px-b">
				<view class="flex-y-center">
					<i class="iconfont c-warning"></i>
					<view class="text-bold">手机号</view>
				</view>
				<input v-model="subForm.phone" type="text" class="pt-md" maxlength="20"
					placeholder-class="c-placeholder" :placeholder="rule[0].errorMsg" />
			</view>
			<view class="mt-lg pb-md b-1px-b">
				<view class="flex-y-center">
					<i class="iconfont c-warning"></i>
					<view class="text-bold">密码</view>
				</view>
				<input v-model="subForm.password" type="password" class="pt-md" maxlength="20"
					placeholder-class="c-placeholder" :placeholder="rule[1].errorMsg" />
			</view>
			
			<view @tap="submit" class="apply-btn flex-center f-sm-title c-base text-bold radius-16"
				:style="{background:primaryColor}">密码登录</view>
				
				<button  open-type="getPhoneNumber" @getphonenumber="get_phone"><view @tap="submit" class="apply-btn flex-center f-sm-title c-base text-bold radius-16"
				:style="{background:primaryColor}">手机号快捷登录</view></button>
				<view class="editpass" @tap="editpass">忘记密码</view>
				<view class="reg" @tap="reg">立即注册</view>
			</button> 
		</view>

		<view class="space-footer">
			
		</view>
	</view>
</template>

<script>
	import {
		mapState,
		mapActions,
		mapMutations
	} from 'vuex';
	import siteInfo from '@/siteinfo.js';
	export default {
		components: {},
		data() {
			return {
				options: {},
				color: '#ffffff',
				code: '',
				subForm: {
					phone: '',
					password: ''
				},
				rule: [{
					name: "phone",
					checkType: "isNotNull",
					errorMsg: "请输入您的手机号码",
					regType: 2
				}, {
					name: "password",
					checkType: "isNotNull",
					errorMsg: "请输入密码",
					regText: "密码"
				}],
				lockTap: false
			}
		},
		computed: mapState({
			primaryColor: state => state.config.configInfo.primaryColor,
			subColor: state => state.config.configInfo.subColor,
			configInfo: state => state.config.configInfo,
			userInfo: state => state.user.userInfo,
			mineInfo: state => state.user.mineInfo,
		}),
		onLoad(options) {
			var that = this;
			uni.login({
				success: function (res) {
					that.code =  res.code;
				}
			})
			this.$util.goUrl({
				url: `/pages/login`,
				openType: `navigateTo`
			})
			console.log(this.userInfo.session_key);
			
			this.options = options;
			if(options.scene){
				this.options.pid = options.scene;
			}
			console.log(this.options);
			if(this.userInfo.session_key){
				
				/*this.$util.goUrl({
					url:`/pages/service`,
					openType: `reLaunch`
				})*/
			}
		},
		onPullDownRefresh() {
			// #ifndef APP-PLUS
			uni.showNavigationBarLoading()
			// #endif
			this.initRefresh()
			uni.stopPullDownRefresh()
		},
		onPageScroll(e) {
			let color = e.scrollTop < 20 ? '#ffffff' : '#000000'
			if (this.color == color) return
			this.color = color
			this.$util.setNavigationBarColor({
				color,
				bg: 'none'
			});
		},
		computed: mapState({
			primaryColor: state => state.config.configInfo.primaryColor,
			subColor: state => state.config.configInfo.subColor,
			configInfo: state => state.config.configInfo,
			autograph: state => state.user.autograph,
			appLogin: state => state.user.appLogin,
			loginType: state => state.user.loginType,
			loginPage: state => state.user.loginPage,
			push_id: state => state.user.push_id,
			userInfo: state => state.user.userInfo,
		}),
		methods: {
			...mapActions(['getConfigInfo', 'getUserInfo', 'getMineInfo', 'getAuthUserProfile']),
			...mapMutations(['updateConfigItem', 'updateUserItem']),
			async initIndex() {},
			initRefresh() {
				
				this.initIndex()
			},
			//表单验证
			validate(param) {
				let validate = new this.$util.Validate();
				this.rule.map(item => {
					let {
						name,
					} = item
					validate.add(param[name], item);
				})
				let message = validate.start();
				console.log(message, "message");
				return message;
			},
			async submit() {
				var that = this;
				let param = this.$util.deepCopy(this.subForm)
				param.code = this.code;
				if(this.options.pid){
					param.pid = this.options.pid;
				}
				let msg = this.validate(param);
				if (msg) {
					this.$util.showToast({
						title: msg
					})
					return
				}
				if (this.lockTap) return
				//this.lockTap = true
				this.$util.showLoading()
				
				let methodModel ='phonelogin';
				let user_info = await this.$api.base[methodModel]({
					data: param
				})
				
				
				this.updateUserItem({
					key: 'appLogin',
					val: user_info.data
				})
				let {
					autograph,
					data
				} = user_info
				this.$util.hideAll()
				this.updateUserItem({
					key: 'userInfo',
					val: data
				})
				this.updateUserItem({
					key: 'autograph',
					val: autograph
				})
				if (!this.configInfo.id) {
					await this.getConfigInfo()
				}
				let {
					phone = ''
				} = data
				phone = 1
				this.$util.goUrl({
					url: phone ? `/pages/service` : `/user/pages/phone`,
					openType: `reLaunch`
				})
			},
			async get_phone(e){
				var that = this;
				uni.login({
					success: function (res) {
						that.code =  res.code;
					}
				})
				console.log(e.detail)
				if (!e.detail.iv) {
					uni.showToast({
						title:'获取手机号失败',
						icon:'none'
					})
					return;
				}
				var that = this;
				var code = e.detail.code;
				var errMsg = e.detail.errMsg;
			
				if(errMsg === 'getPhoneNumber:ok'){
					let methodModel ='getPhoneNumber';
					let res = await this.$api.base[methodModel]({
						data: {code:code}
					})
					methodModel ='phonelogin';
					var pid = '';
					if(this.options.pid){
						pid = this.options.pid;
					}
					let user_info = await this.$api.base[methodModel]({
						data: {'type':'weixin','phone':res,'code':that.code,'pid':pid}
					})
					let {
						autograph,
						data
					} = user_info
					this.$util.hideAll()
					this.updateUserItem({
						key: 'userInfo',
						val: data
					})
					this.updateUserItem({
						key: 'autograph',
						val: autograph
					})
					var phone = 1;
					this.$util.goUrl({
						url: phone ? `/pages/service` : `/user/pages/phone`,
						openType: `reLaunch`
					})
					// uni.request({ //获取用户手机号
					// 	url: 'https://oa.yuxiang123.cn/api/index/getPhoneNumber?code='+code,
					// 	method: 'GET',
					// 	success: function (res) {
					// 		if(res.data.code == 1 ){
					// 			var mobile = res.data.data;
					// 			uni.login({
					// 				success: function (res) {
					// 					if(res.code) {
					// 						let invite_user_id=uni.getStorageSync('invite_user_id')?uni.getStorageSync('invite_user_id'):0;
															
					// 					}
					// 				}					
					// 			});

					// 		}else{
					// 			uni.showToast({
					// 				title:'获取手机号失败,请重试!',
					// 				icon:'none'
					// 			})
					// 			return;
					// 		}
					// 	}
					// });
				}
			},
			reg(){
				var pid = '';
				if(this.options.pid){
					pid = this.options.pid;
				}
				this.$util.goUrl({
					url: `/pages/register?pid=`+pid,
					openType: `navigateTo`
				})
			},
			editpass(){
				this.$util.goUrl({
					url: `/pages/editpass`,
					openType: `navigateTo`
				})
			},
		}
	}
</script>

<style lang="scss">
	.wx_phone{
		position: relative;
		left: 500rpx;
		width: 80rpx;
		image{
			width: 70rpx;
			height: 70rpx;
			margin-top: 20rpx;
		}
	}
	.mine-distribution-index {
		.space-user-info {
			top: 0;
			left: 0;
			width: 100%;
			height: 219rpx;
		}
		.mine-index-bg {
			background: rgb(57, 181, 74);
			height: 14.625rem;
			width: 140%;
			left: -20%;
			top: 0;
			
		}
		.apply-info {
			padding: 50rpx;

			.apply-btn {
				width: 590rpx;
				height: 96rpx;
				margin-top: 20rpx;
			}
		}

	}
	
	.reg{
		margin-top:30rpx;
		float:right;
	}
	.editpass{
		margin-top:30rpx;
		float:left;
	}
</style>
